package com.luobd.bpm.controller;

import cn.hutool.core.util.StrUtil;
import com.luobd.bpm.common.PageInput;
import com.luobd.bpm.dto.ProcessInstanceDTO;
import com.luobd.bpm.input.ProcessStartInput;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceQuery;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/api/runtime")
@Api(tags = "流程运行时管理")
public class BpmnRuntimeController {


    private final RuntimeService runtimeService;


    public BpmnRuntimeController(RuntimeService runtimeService) {
        this.runtimeService = runtimeService;
    }



    @PostMapping(value = "/start")
    @ApiOperation(value = "启动流程实例")
    public String startProcessInstance(@RequestBody ProcessStartInput input){
       return   runtimeService
                .createProcessInstanceBuilder().processDefinitionId(input.getProcessDefinitionId())
                .processDefinitionKey(input.getProcessDefinitionKey())
                .businessKey(input.getBusinessKey())
                .variables(input.getVariables())
                .start().getId();
    }


    @GetMapping(value = "/sendSignal")
    @ApiOperation(value = "发送信号")
    public String sendSignal(@RequestParam(value = "signalName") String signalName){
        runtimeService.signalEventReceived(signalName);
        return "ok";
    }



    @GetMapping(value = "/allInstance")
    @ApiOperation(value = "获取所有流程实例")
    public List<ProcessInstanceDTO> getAllProcessInstances(){
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().list();
        return list.stream().map(ProcessInstanceDTO::new).collect(Collectors.toList());
    }



    @PostMapping(value = "/searchInstance")
    @ApiOperation(value = "搜索流程")
    public List<ProcessInstanceDTO> searchProcessInstance(@RequestBody ProcessInstanceQueryInput input){
        ProcessInstanceQuery query = runtimeService.createProcessInstanceQuery();
        if(StrUtil.isNotEmpty(input.getProcessDefinitionId())){
            query.processDefinitionId(input.getProcessDefinitionId());
        }
        if(StrUtil.isNotBlank(input.getProcessDefinitionKey())){
            query.processDefinitionKey(input.getProcessDefinitionKey());
        }
        if(StrUtil.isNotBlank(input.getProcessDefinitionName())){
            query.processInstanceNameLike(input.getProcessDefinitionName());
        }
        return query.listPage(input.getStart(),input.getPageSize()).stream().map(ProcessInstanceDTO::new).collect(Collectors.toList());
    }



}

@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel(description = "流程实例查询条件")
class ProcessInstanceQueryInput extends PageInput {


    @ApiModelProperty(value = "流程定义id")
    private String processDefinitionId;


    @ApiModelProperty(value = "流程定义key")
    private String processDefinitionKey;

    @ApiModelProperty(value = "流程定义名称")
    private String processDefinitionName;




}

